import type {Model} from "./model/Model.ts";
import type {Object3D, Texture} from "three";

export class ModelInstanceStore {
    private readonly r: Map<string, Model>
    private readonly texture: Map<string, Texture>

    constructor() {
        this.r = new Map<string, Model>()
        this.texture = new Map<string, Texture>()
    }

    getModels(): Map<string, Model> {
        return this.r
    }

    addModel(name: string, model: Model): void {
        this.r.set(name, model)
    }

    getModel(name: string): Model | undefined {
        let m = this.r.get(name)
        if (m == undefined) {
            return undefined
        }
        let model = m.clone()
        model.scene = m.scene?.clone(true)
        model.modelName = m.modelName
        model.updateEnable = m.updateEnable
        model.layer = m.layer
        if (model.layer.length > 0) {
            let firstLayer = model.layer[0]
            model.scene?.traverse((child: Object3D) => {
                if (!child) {
                    return
                }
                child.layers.set(firstLayer)
                if (model.layer.length > 1) {
                    for (let layerNumber of model.layer) {
                        child.layers.enable(layerNumber)
                    }
                }
            })
        }

        model.modelStore = this
        return model
    }

    addTexture(name: string, texture: Texture): void {
        this.texture.set(name, texture)
    }

    getTexture(name: string): Texture | undefined {
        return this.texture.get(name)
    }
}